package com.magold.start.credits;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Scanner;

import org.andengine.entity.IEntity;
import org.andengine.entity.particle.ParticleSystem;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.entity.sprite.Sprite;

import android.content.res.AssetManager;

public class TextEntityParser {
	public static interface TextEntityInflater {
		public IAreaShape createItem(String pValue, boolean pHighLight);
	}
	
	public static interface TextEntityAnimator {
		public void createModifier(IAreaShape pTarget, float pStartTime, float pToX, float pToY, int pIndex);
		public ParticleSystem<Sprite> createTextParticleSystem(IAreaShape pTarget, float pStartTime, int pIndex);
		public float getDuration();
		public float getMoveDuration();
	}

	private final float mStartTime;
	private final float mInitY, mInitX;
	private final float mHeightSpan, mWidthSpan;
	private final float mCharSpace;
	private TextEntityInflater mInflater;
	private TextEntityAnimator mAnimator;
	
	public TextEntityParser(TextEntityInflater pInflater, TextEntityAnimator pAnimator, float pStartTime, float pInitX, float pInitY, float pHeightSpan, float pWidthSpan, float pCharSpace) {
		this.mInflater = pInflater;
		this.mAnimator = pAnimator;
		this.mStartTime = pStartTime;
		this.mInitX = pInitX;
		this.mInitY = pInitY;
		this.mHeightSpan = pHeightSpan;
		this.mWidthSpan = pWidthSpan;
		this.mCharSpace = pCharSpace;
	}
	
	/**
	 * 
	 * @param pAssetManager
	 * @param pAssetPath
	 * @param pContainer
	 * @return duration of animation.
	 */
	public float buildTexts(AssetManager pAssetManager, String pAssetPath, List<IEntity> pContainer) {
		InputStream in;
		try {
			in = pAssetManager.open(pAssetPath);
		} catch (IOException e) {
			e.printStackTrace();
			return 0;
		}
		
		return buildTexts(in, pContainer);
	}

	public float buildTexts(InputStream in, List<IEntity> pContainer) {
		Scanner scanner = new Scanner(in);
		
		float startTime = mStartTime;
		float toX = mInitX, toY = mInitY;
		boolean isTitle = true;
		while (scanner.hasNextLine()) {
			String line = scanner.nextLine();
			
			String realLine = line.trim();
			if (!realLine.equals("")) {
				float dh = buildTexts(realLine, isTitle, toX, toY, startTime, pContainer);
				toX += (isTitle ? mWidthSpan : 0.0f);
				isTitle = false;
				toY = (toY + dh + mHeightSpan);
//				startTime += mAnimator.getMoveDuration();
			} else {
				isTitle = true;
				toY = mInitY;
				toX = mInitX;
				startTime += mAnimator.getDuration();
			}
		}
		
		return startTime;
	}

	private float buildTexts(String pLine, boolean pHighLight, float pToX, float pToY, float pStartTime, List<IEntity> pBuiltEntities) {
		float maxHeight = 0;
		for (int i = 0; i < pLine.length(); i++) {
			IAreaShape single = mInflater.createItem(pLine.substring(i, i+1), pHighLight);
			
			if (single == null) continue;
			
			pBuiltEntities.add(single);
			mAnimator.createModifier(single, pStartTime, pToX, pToY, i);
			IEntity particleForThis = mAnimator.createTextParticleSystem(single, pStartTime, i);
			pBuiltEntities.add(particleForThis);
			pToX += (single.getWidth() + mCharSpace);
			if (maxHeight < single.getHeight()) {
				maxHeight = single.getHeight();
			}
		}
		
		return maxHeight;
	}
	
}
